/*
 * Copyright (c) 2011  Cristian Constantin
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @author: Cristian Constantin
 * @date: 12/23/11
 */


Ext.define('Quizio.controller.QuizPlayer', {
    extend: 'Ext.app.Controller',

    views: [
        'QuizPlayerWindow',
        'QuizPanel',
        'QuizSummaryPanel',
        'QuestionsListPanel'
    ],

    stores: [
        'QuizSessionQuestions'
    ],

    refs: [
        {
            ref: 'quizPlayerWindow',
            selector: 'quizplayerwindow'
        },
        {
            ref: 'quizPanel',
            selector: 'quizpanel'
        },
        {
            ref: 'questionsList',
            selector: 'questionslistpanel'
        },
        {
            ref: 'summaryPanel',
            selector: 'quizsummarypanel'
        }
    ],


    init: function() {

        this.control({
            'sessionslistpanel': {
                itemclick: this.playTest
            },
            'questionslistpanel': {
                itemclick: this.onQuestionClick
            },
            'quizpanel button[action=quit]': {
                click: this.closeTest
            },
            'quizpanel button[action=submit]': {
                click: this.submitAnswer
            },
            'quizpanel button[action=submit-quiz]': {
                click: this.submitQuiz
            },
            'quizpanel button[action=next]': {
                click: this.nextQuestion
            }
        });

        this.application.on('quiztimeout', this.submitQuiz, this);
    },

    playTest: function(itemsList, quizSession) {
        if (quizSession.get("active")) {
            this.openQuizPlayer();
        }
        else {
            Ext.ux.Ajax.request({
                url: 'quiz-player/init-session.do',
                params: {
                    sessionId: quizSession.getId()
                },
                success: this.openQuizPlayer,
                scope: this
            });
        }
    },

    openQuizPlayer: function() {
        Ext.ux.Ajax.request({
            url: 'quiz-player/load-quiz.do',
            success: function(quizData) {
                Ext.create('Quizio.view.QuizPlayerWindow').show();
                this.loadCurrentQuestion();
                this.quizData = quizData;
                this.getQuizSessionQuestionsStore().loadData(quizData.questions);
                this.application.fireEvent('quizloaded', quizData);
                this.getQuestionsList().select(quizData.currentQuestion - 1)
            },
            scope: this
        });
    },


    closeTest: function() {
        this.getQuizPlayerWindow().close();
    },

    loadCurrentQuestion: function() {
        Ext.ux.Ajax.request({
            url: 'quiz-player/current-question.do',
            success: function(obj) {
                this.getQuizPanel().loadQuestion(obj);
            },
            scope: this
        })
    },

    onQuestionClick: function(view, record, el, index) {
        Ext.ux.Ajax.request({
            url: 'quiz-player/go-to-question.do',
            scope: this,
            loadMask: true,
            params: {
                index: index + 1
            },
            success: this.loadCurrentQuestion
        })
    },

    nextQuestion: function() {
        Ext.ux.Ajax.request({
            url: 'quiz-player/go-to-question.do',
            scope: this,
            params: {
                index: this.getQuestionsList().getSelectionModel().getLastSelected().get("id")
            }
        })
    },

    submitQuiz: function() {
        Ext.ux.Ajax.request({
            url: 'quiz-player/submit-quiz.do',
            loadMask: true,
            callback: this.closeTest,
            scope: this
        });
    },

    submitAnswer: function() {

        var userInput = this.getQuizPanel().getUserInput();

        var doSubmit = function() {
            Ext.ux.Ajax.request({
                url: 'quiz-player/submit-answer.do',
                params: userInput
            });
        };

        if (!userInput.answer) {
            Ext.MessageBox.confirm("Warning",
                "You did not enter any answer. Are you sure you want to submit?",
                function(btn) {
                    if (btn == "yes") {
                        doSubmit();
                    }
                })
        }
        else {
            doSubmit();
        }
    }

});
